package de.fmannan.addbook.editor;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import de.fmannan.addbook.common.fieldtypes.IField;
import de.fmannan.addbook.common.fieldtypes.IStringField;
import de.fmannan.addbook.domainmodel.Group;
import de.fmannan.addbook.domainmodel.GroupRegistrar;

/**
 * 
 * @author fmannan
 * 
 * 
 * Provides an EditorInputAdapter-Implementation to modify a Group and its
 * settings.
 */
public class GroupEditorInputAdapter implements IEditorInput, IGenericEditorInput{

	private static Logger log = Logger.getLogger(GroupEditorInputAdapter.class);
    private Group group;
    private List<IField> fieldsList;
    
    /**
	 * Create the adapter. The fieldList determines which fields have to be
	 * displayed by the Editor.
	 * 
	 * @param adaptableObject
	 *            Object to be adapted.
	 * @param adapterType
	 *            Type of the adapter to create.
	 */
    public GroupEditorInputAdapter(Object adaptableObject, Class adapterType) {
        super();
        this.group = (Group) adaptableObject;
        // Create a new ArrayList with fields that determines the Groups
		// possible modification-options
        fieldsList = new ArrayList<IField>();
        
        // Field to set the name of the Group
        fieldsList.add(new IStringField() {
            public String getLabel() {
                return "Groupname";
            }
            public String getValue() {
                return group.getGroupName();
            }
            public void setValue(String value)  {
                group.setGroupName(value);
            }
        });
        fieldsList.add(new IStringField() {
            public String getLabel() {
                return "Group Description";
            }
            public String getValue() {
                return group.getGroupDescription();
            }
            public void setValue(String value)  {
                group.setGroupDescription(value);
            }
        });        
/*        
        fieldsList.add(new IStringSelectField() {
            public String getLabel() {
                return "Wartungsvorschrift";
            }
            public String getValue() {
                return group.getMaintenanceRule();
            }
            public void setValue(String value)  {
                group.setMaintenanceRule(value);
            }
            public String[] getSelectables() {
            	return new String[] {"keine", "Druck", "Schutzleiter", "Sitzpr�fung"};
            }
        });
*/
    }

    public boolean exists() {
        return true;
    }

    public ImageDescriptor getImageDescriptor() {
        return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
    }

    public String getName() {
        String name = group.getGroupName();
    	if (name == null) {
    		name = "Neu";
    	}
        return name;
    }

    public IPersistableElement getPersistable() {
        // editor input not persistable
        return null;
    }

    public String getToolTipText() {
        return "Modify Group"; 
    }

    public Object getAdapter(Class adapter) {
        return Platform.getAdapterManager().getAdapter(this, adapter);
    }

    public List<IField> getFieldsList() {
        return fieldsList;
    }
    
    public Group getGroup() {
        return group;
    }
   
/*
    public void addEntityEventListener(IEntityEventListener listener) {
        getGroup().addEntityEventListener(listener);
    }

    public void removeEntityEventListener(IEntityEventListener listener) {
        getGroup().removeEntityEventListener(listener);
    }
*/    
    public boolean equals(Object obj) {
        boolean result;
        
        if ( obj == null ) {
            result = false;
        }
        else if ( ! (obj instanceof GroupEditorInputAdapter ) ) {
            result = false;
        }
        else {
        	GroupEditorInputAdapter castedObj = (GroupEditorInputAdapter) obj;
            result = this.group.equals(castedObj.group);
        }
        
        return result;
    }

    public int hashCode() {
        return group.hashCode();
    }

    public String getTitle() {
        return "Group Details"; //$NON-NLS-1$
    }

    public Object getAdaptedObject() {
        return group;
    }

	public void commit()
	{
		if(this.group.isNewlyCreated()) {
			log.debug("Attempting to ad newly created group to the GroupRegistrar");
			GroupRegistrar.getInstance().addChild(group);
		} 
	}
}
